<template>
    <ExternalLinkTip :is-show="isExternalLink" />
    <Search :title="title" @submit="getAllData" />
    <Quantity ref="QuantityRef" :is-external-link="isExternalLink" />
    <shareTop
        class="share"
        :is-external-link="isExternalLink"
        :staff-data="staffData"
        :customer-data="customerData"></shareTop>
    <Statistics ref="StatisticsRef" :is-external-link="isExternalLink" @submit="getLineData" />
    <Table ref="TableRef" :is-external-link="isExternalLink" @submit="getTableData" @TableExport="onExport" />
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
import {
    getBoardBasic,
    getBoardBrowse,
    getBoardTag,
    getBoardTrend,
    getBoardList,
    getBoardExport,
    custContentTop,
    staffContentTop
} from '@/api/content/contentCenter/board';
import { useRoute } from 'vue-router';
import { getDay, DFormat } from '@/utils/utils';
import { DOWLOAD_FILE } from '@/utils/smartMarketing';
import Search from './components/Search.vue';
import Quantity from './components/Quantity.vue';
import Statistics from './components/Statistics.vue';
import shareTop from './components/shareTop.vue';
import Table from './components/Table.vue';
import ExternalLinkTip from './components/ExternalLinkTip.vue';

const QuantityRef: any = ref(null);
const StatisticsRef: any = ref(null);
const TableRef: any = ref(null);
const route = useRoute();
const pageType = ref(1);
const staffData = ref([]); //员工分享top10
const customerData = ref([]); //客户分享top10
const title = route.query.title;
const isExternalLink = computed(() => {
    return route.query.is_external_link === 'true' ? true : false;
});
const defaultParams = ref({
    content_id: route.query.id,
    work_user_id: [],
    filter_time: [getDay(-6), getDay(0, true)] //创建时间
});
// 获取全部数据
const getAllData = (v?: any) => {
    if (v && v.filter_time) defaultParams.value = { ...defaultParams.value, ...v };
    Promise.all([
        getBoardBasic(defaultParams.value),
        getBoardBrowse(defaultParams.value),
        getBoardTag(defaultParams.value),
        getBoardTrend({ ...defaultParams.value, type: pageType.value }),
        getBoardList({ ...defaultParams.value, page: 1, page_size: 15 }),
        custContentTop(defaultParams.value),
        staffContentTop(defaultParams.value)
    ]).then(res => {
        QuantityRef.value.getState(res[0]);
        StatisticsRef.value.getEchartData([res[1], res[2], res[3]]);
        TableRef.value.onTableData(res[4]);
        customerData.value = res[5];
        staffData.value = res[6];
    });
};
// 获取折线图数据
const getLineData = async (v: any) => {
    pageType.value = v;
    const res = await getBoardTrend({ ...defaultParams.value, type: v });
    StatisticsRef.value.onUpdateLine(res);
};
// 获取表格数据
const getTableData = async (v: any) => {
    const res = await getBoardList({ ...defaultParams.value, ...v });
    TableRef.value.onTableData(res);
};
// 导出
const onExport = async (v: any) => {
    const res = await getBoardExport({ ...defaultParams.value, ...v });
    DOWLOAD_FILE(res.url, res.fileName);
};
onMounted(() => {
    getAllData();
});
</script>

<style lang="scss" scoped>
.share {
    margin-top: 16px;
}
</style>
